package com.shzq.service.impl;

import com.shzq.constant.GradeEnum;
import com.shzq.dao.PointMapper;
import com.shzq.service.PointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PointServiceImpl implements PointService {

    @Autowired
    private PointMapper pointMapper;
    @Override
    public Map<String,List<List<Double>>> getAllPoint() {
        Map<String,List<List<Double>>> resultMap = new HashMap<>();
        GradeEnum[] values = GradeEnum.values();
        for (GradeEnum value : values) {
            resultMap.put(value.getName(),selectByDepth(value));
        }
        return resultMap;
    }

    @Override
    public Map<String,List<List<Double>>> getAllPointOrderByStrength(){
        Map<String,List<List<Double>>> resultMap = new HashMap<>();
        GradeEnum[] values = GradeEnum.values();
        for (GradeEnum value : values) {
            resultMap.put(value.getName(),selectByStrength(value));
        }
        return resultMap;
    }

    @Override
    public List<List<Double>> getPointByStrength(String grade) {
        GradeEnum[] values = GradeEnum.values();
        for (GradeEnum value : values) {
            if(value.getName().equals(grade)){
                return selectByStrength(value);
            }
        }
        return null;
    }

    @Override
    public List<List<Double>> getPointByDepth(String grade) {
        GradeEnum[] values = GradeEnum.values();
        for (GradeEnum value : values) {
            if(value.getName().equals(grade)){
                return selectByDepth(value);
            }
        }
        return null;
    }

    private List<List<Double>> selectByDepth(GradeEnum grade){
        Integer begin = grade.getDepth()[0];
        Integer end = grade.getDepth()[1];
        Map<String,Integer> parMap = new HashMap<>();
        parMap.put("begin",begin);
        parMap.put("end",end);
        List<List<Double>> resultList = new ArrayList<>();
        List<String> stringResult;
        if(end!=null)
        stringResult = pointMapper.findByDepth(parMap);
        else stringResult = pointMapper.findMoreThanDepth(begin);
        pareString(stringResult,resultList);
        return resultList;
    }
    private List<List<Double>> selectByStrength(GradeEnum grade){
        Integer begin = grade.getStrength()[0];
        Integer end = grade.getStrength()[1];
        Map<String,Integer> parMap = new HashMap<>();
        parMap.put("begin",begin);
        parMap.put("end",end);
        List<List<Double>> resultList = new ArrayList<>();
        List<String> stringResult;
        if(end==null)
            stringResult = pointMapper.findMoreThanStrength(begin);
        else if(begin==null)
            stringResult = pointMapper.findLessThanStrength(end);
        else stringResult = pointMapper.findByStrength(parMap);
        pareString(stringResult,resultList);
        return resultList;
    }
    @Override
    public void pareString(List<String> strList,List<List<Double>> resultList){
        for (String s : strList) {
            String[] split = s.split(":");
            if(split.length<2) {
                System.out.println(s);
                break;
            }
            if (split[0].length()>6) split[0] = split[0].substring(0, 5);
            if(split[1].length()>6)split[1] = split[1].substring(0,5);
            List<Double> ld = new ArrayList<>();
            ld.add(Double.parseDouble(split[0]));
            ld.add(Double.parseDouble(split[1]));
            resultList.add(ld);
        }
    }
}
